package com.skzh.web.controller.unflyzone;

import com.skzh.common.annotation.Log;
import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.core.page.TableDataInfo;
import com.skzh.common.enums.BusinessType;
import com.skzh.unflyzone.domain.DbxPoints;
import com.skzh.unflyzone.domain.DbxPolygon;
import com.skzh.unflyzone.domain.dto.DbxCreateDTO;
import com.skzh.unflyzone.domain.dto.PointDTO;
import com.skzh.unflyzone.domain.dto.PolygonWithPointsDTO;
import com.skzh.unflyzone.service.IDbxUnflyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zr
 * @Description 多边形禁飞区控制器
 * @Date 2025/3/26 9:45
 */
@RequestMapping("/dbxunflyzone/dbxInfo")
@RestController
public class DbxController extends BaseController {

    @Autowired
    private IDbxUnflyService dbxUnflyService;

    /**
     * 新增多边形禁飞区
     */
    @ApiOperation(value = "新增多边形禁飞区")
    @Log(title = "多边形禁飞区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DbxCreateDTO createDTO) {
        return toAjax(dbxUnflyService.insertDbxUnfly(createDTO));
    }

    /**
     * 查询多边形禁飞区
     */
    @ApiOperation(value = "查询多边形禁飞区")
    @GetMapping("/list")
    public TableDataInfo list(DbxPolygon dbxPolygon) {
        if (dbxPolygon!=null){
            List<DbxPolygon> polygonList = dbxUnflyService.selectDbxList(dbxPolygon);
            // 取出polygonList所有元素的id
            List<Long> dbxIds = polygonList.stream().map(DbxPolygon::getId).collect(Collectors.toList());
            // 根据ID批量查询对应多边形的顶点信息
            List<DbxPoints> dbxPointsList = dbxUnflyService.selectPointByIds(dbxIds);


            // 创建一个Map来存储每个多边形ID对应的顶点信息
            Map<Long, List<DbxPoints>> pointsMap = dbxPointsList.stream()
                    .collect(Collectors.groupingBy(DbxPoints::getDbxId));

            // 创建一个列表来存储组合后的多边形及其顶点信息
            List<PolygonWithPointsDTO> polygonWithPointsList = new ArrayList<>();
            for (DbxPolygon polygon : polygonList) {
                PolygonWithPointsDTO polygonWithPointsDTO = new PolygonWithPointsDTO();
                polygonWithPointsDTO.setPolygon(polygon);
                List<DbxPoints> dbxPoints = pointsMap.getOrDefault(polygon.getId(), Collections.emptyList());
                polygonWithPointsDTO.setPoints(dbxPoints.stream()
                        .map(point -> {
                            PointDTO pointDTO = new PointDTO();
                            pointDTO.setPointLongitude(point.getPointLongitude());
                            pointDTO.setPointLatitude(point.getPointLatitude());
                            pointDTO.setPointOrder(point.getPointOrder());
                            return pointDTO;
                        })
                        .collect(Collectors.toList()));
                polygonWithPointsList.add(polygonWithPointsDTO);
            }

            return getDataTable(polygonWithPointsList);
        }
            return null;
    }

    /**
     * 根据ID删除多边形禁飞区
     */
    @ApiOperation(value = "删除多边形禁飞区")
    @Log(title = "多边形禁飞区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(dbxUnflyService.deleteDbxById(id));
    }

}
